/*
 * Copyright (c) 2017. 深圳掌控网络有限公司. All rights reserved.
 */

package com.yiqi.lottery.feature.lottery.presenter;

import com.suozhang.framework.entity.bo.PageData;
import com.yiqi.lottery.entity.bo.CouprceBo;
import com.yiqi.lottery.entity.bo.HallBo;
import com.yiqi.lottery.entity.bo.PayBo;
import com.yiqi.lottery.feature.lottery.contract.HallContract;
import com.yiqi.lottery.feature.lottery.contract.HomeContract;
import com.yiqi.lottery.feature.lottery.dependencies.hall.DaggerHallPresenterComponent;
import com.yiqi.lottery.feature.lottery.dependencies.home.DaggerHomePresenterComponent;

import java.util.HashMap;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import javax.inject.Inject;

/**
 *      LIJUWEN
 * @email yiyayiyayaoljw@gmail.com
 * @date 2017/4/11 9:31
 */
public class HallPresenter implements HallContract.Presenter {

    private HallContract.View mView;

    @Inject

    HallContract.Model mModel;


    public HallPresenter(HallContract.View mView) {
        this.mView = mView;
        DaggerHallPresenterComponent.create().inject(this);
    }

    @Override
    public void start() {

    }


    @Override
    public void getHallList(int page,int sort, Integer bet_type) {
        mModel.getHallList(page, sort,bet_type).compose(mView.bindToLife()).subscribe(new Observer<PageData<CouprceBo>>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(PageData<CouprceBo> stringPageData) {
                mView.dismissLoading();
                if (stringPageData.getTotal()==0){
                   mView.showHallListEmpty();
                }else {
                mView.showHallListSuccess(stringPageData);}
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showHallListError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void getHallMoreList(int page,int sort, Integer bet_type) {
        mModel.getHallList(page,sort,bet_type).compose(mView.bindToLife()).subscribe(new Observer<PageData<CouprceBo>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(PageData<CouprceBo> stringPageData) {

                    mView.showHallListMoreSuccess(stringPageData);
            }

            @Override
            public void onError(Throwable e) {
                mView.showHallListMoreError(e.getMessage());
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void getHallDetail(String id) {
        mModel.getHallDetail(id).compose(mView.bindToLife()).subscribe(new Observer<CouprceBo>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(CouprceBo stringPageData) {
                mView.dismissLoading();
               mView.showHallDetailSuccess(stringPageData);
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showHallListError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void createCopurch(int type, int multiple,int mut_num, String bet_code, String issue, String amount_fee, String share,
                              String percentage, String subscribe, String guarantee, String declaration, String desc, String finish_time) {
        HashMap<String,Object> map=new HashMap<>();
        map.put("type",type);
        map.put("multiple",multiple);
        map.put("mut_num",mut_num);
        map.put("bet_code",bet_code);
        map.put("issue",issue);
        map.put("amount_fee",amount_fee);
        map.put("share",share);
        map.put("percentage",percentage);
        map.put("subscribe",subscribe);
        map.put("guarantee",guarantee);
        map.put("declaration",declaration);
        map.put("desc",desc);
        map.put("finish_time",finish_time);
        mModel.createCopurch(map).compose(mView.bindToLife()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(String stringPageData) {
                mView.dismissLoading();
                mView.showCopurchSubmitSuccess(stringPageData);
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showHallListError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void copurchSubmit(Integer copurce_id, Integer share) {

        CouprceBo couprceBo=new CouprceBo();
        couprceBo.setCopurce_id(copurce_id);
        couprceBo.setShare(share);
        mModel.copurchSubmit(couprceBo).compose(mView.bindToLife()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(String stringPageData) {
                mView.dismissLoading();
                mView.showCopurchSubmitSuccess(stringPageData);
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showHallListError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void copurchToPay(int pay_type, Integer copurce_list_id) {
        PayBo payBo=new PayBo(pay_type,copurce_list_id);
        mModel.copurchToPay(payBo).compose(mView.bindToLife()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(String stringPageData) {
                mView.dismissLoading();
                mView.showCopurchPaySuccess(stringPageData);
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showHallListError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void cancleCopurch(String id) {
        mModel.cancleCopurch(id).compose(mView.bindToLife()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(String stringPageData) {
                mView.dismissLoading();
                mView.showCancleSuccess();
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showCancleError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }
}
